﻿using EA.Common;
using EA.DAO;
using EA.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EA.BA
{
    public class MemberBA : ObjectBA
    {
        public MemberTO GetMemberByUsername(String username)
        {
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            DataTable table = memberDA.GetMemberByUsername(username);
            MemberTO memberTO = new MemberTO();
            if (table.Rows.Count != 0)
            {
                memberTO.ID = Convert.ToInt32(table.Rows[0]["ID"]);
                memberTO.FullName = table.Rows[0]["FullName"].ToString();
                memberTO.UserName = table.Rows[0]["UserName"].ToString();
                memberTO.Role = Convert.ToInt32(table.Rows[0]["Role"]);
                memberTO.Password = table.Rows[0]["Password"].ToString();
                memberTO.LastLogOnDate = Converts.getValueDateTime("LastLogOnDate", 0, table);
            }
            return memberTO;
        }

        public Boolean DeleteMember(int ID)
        {
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            return memberDA.DeleteMember(ID);
        }

        public Boolean UpdateMember(MemberTO member)
        {
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            return memberDA.UpdateMember(member);
        }

        public List<MemberTO> GetAllMember()
        {
            List<MemberTO> list = new List<MemberTO>();
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            DataTable table1 = memberDA.GetListMember();
            for (int i = 0; i < table1.Rows.Count; i++)
            {
                MemberTO memberTO = new MemberTO();
                memberTO.UserName = table1.Rows[i]["UserName"].ToString();
                memberTO.FullName = table1.Rows[i]["FullName"].ToString();
                memberTO.Role = Convert.ToInt32(table1.Rows[i]["Role"]);
                memberTO.ID = Convert.ToInt32(table1.Rows[i]["ID"]);
                memberTO.LastLogOnDate = Converts.getValueDateTime("LastLogOnDate", i, table1);
                list.Add(memberTO);
            }
            return list;
        }

        public MemberTO GetMemberByID(int ID)
        {
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            DataTable table = memberDA.GetMemberByID(ID);
            MemberTO memberTO = new MemberTO();
            if (table.Rows.Count != 0)
            {
                memberTO.ID = Convert.ToInt32(table.Rows[0]["ID"]);
                memberTO.FullName = table.Rows[0]["FullName"].ToString();
                memberTO.UserName = table.Rows[0]["UserName"].ToString();
                memberTO.Role = Convert.ToInt32(table.Rows[0]["Role"]);
                memberTO.ID = Convert.ToInt32(table.Rows[0]["ID"]);
                memberTO.LastLogOnDate = Converts.getValueDateTime("LastLogOnDate", 0, table);
            }
            return memberTO;
        }

        public Boolean InsertMember(MemberTO member)
        {
            MemberDA memberDA = new MemberDA(ConnectionString);
            return memberDA.InsertMember(member);
        }

        public Boolean UpdateLastLogOnDate(int ID)
        {
            MemberDA memberDA = new MemberDA(ObjectBA.ConnectionString);
            return memberDA.UpdateLastLogOnDate(ID);
        }
    }
}
